/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;

/**
 *
 * @author James
 */
@Entity
public class Item implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long    id;
    private String  name;
    private String  description;
    private int     type;
    private int     lastMonthNumOfBuyers;
    private int     numOfBuyers;  // Number of people bought this item, this one is for checking which item is the most popular
    private boolean selling;
    private boolean active;
    private boolean deleted;
    private long    createdDate;
    @OneToOne(cascade={CascadeType.PERSIST})
    private ItemDetails latestItemDetails;
    @OneToMany(cascade={CascadeType.PERSIST})
    private List<ItemDetails> listOfItemDetails;
    @ManyToOne
    private ItemCategory theCategory;
    @OneToMany(cascade={CascadeType.ALL})
    private List<Files> listOfPictures;
    
    public Item() {
        this.lastMonthNumOfBuyers = 0;
        this.numOfBuyers          = 0;
        this.active               = false;
        this.deleted              = false;
        this.createdDate          = System.currentTimeMillis();
        this.listOfPictures       = new ArrayList<Files>();
        this.listOfItemDetails    = new ArrayList<ItemDetails>();
    }
    
    public void createItem(String name, String description, int type, int discount, int price, int redeemPrice, int additionalLoyaltyPoint, boolean selling, ItemCategory theCategory, List<Item> subItems) {
        this.name        = name;
        this.description = description;
        this.type        = type;
        this.selling     = selling; 
        this.theCategory = theCategory;
        ItemDetails details    = new ItemDetails();
        details.createItemDetails(discount, price, redeemPrice, additionalLoyaltyPoint, subItems);
        this.listOfItemDetails.add(details);
        this.latestItemDetails = details;
    }
    
    public void addPicture(Files thePicture) {
        this.listOfPictures.add(thePicture);
    }
    
    //Getters and Setters
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public boolean isSelling() {
        return selling;
    }

    public void setSelling(boolean selling) {
        this.selling = selling;
    }

    public int getLastMonthNumOfBuyers() {
        return lastMonthNumOfBuyers;
    }

    public void setLastMonthNumOfBuyers(int lastMonthNumOfBuyers) {
        this.lastMonthNumOfBuyers = lastMonthNumOfBuyers;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    
    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public int getNumOfBuyers() {
        return numOfBuyers;
    }

    public void setNumOfBuyers(int numOfBuyers) {
        this.numOfBuyers = numOfBuyers;
    }

    public boolean isActive() {
        return active;
    }

    public void setActive(boolean active) {
        this.active = active;
    }

    public long getCreatedDate() {
        return createdDate;
    }

    public void setCreatedDate(long createdDate) {
        this.createdDate = createdDate;
    }

    public ItemCategory getCategory() {
        return theCategory;
    }

    public void setCategory(ItemCategory theCategory) {
        this.theCategory = theCategory;
    }

    public List<Files> getListOfPictures() {
        return listOfPictures;
    }

    public void setListOfPictures(List<Files> listOfPictures) {
        this.listOfPictures = listOfPictures;
    }

    public boolean isDeleted() {
        return deleted;
    }

    public void setDeleted(boolean deleted) {
        this.deleted = deleted;
    }

    public List<ItemDetails> getListOfItemDetails() {
        return listOfItemDetails;
    }

    public void setListOfItemDetails(List<ItemDetails> listOfItemDetails) {
        this.listOfItemDetails = listOfItemDetails;
    }

    public ItemDetails getLatestItemDetails() {
        return latestItemDetails;
    }

    public void setLatestItemDetails(ItemDetails latestItemDetails) {
        this.latestItemDetails = latestItemDetails;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Item)) {
            return false;
        }
        Item other = (Item) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "Entity.Item[ id=" + id + " ]";
    }
    
}
